package com.baizan.daka.ui.activity

import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import androidx.appcompat.app.AlertDialog
import com.baizan.base.BaseActivity
import com.baizan.daka.*
import com.baizan.daka.mvp.model.bean.AppVerSionBean
import com.baizan.daka.mvp.model.bean.BankCardInfoBean
import com.baizan.daka.mvp.model.bean.LoginInfoBean
import com.baizan.daka.ui.contract.SettingContract
import com.baizan.daka.ui.presenter.LoginOutPresenter
import com.baizan.daka.util.AppUtils
import com.baizan.daka.util.Preference
import com.baizan.daka.util.ToastUtils
import com.dianzan.daka.R
import kotlinx.android.synthetic.main.act_setting.*
import kotlinx.android.synthetic.main.frg_me.*

class SettingActivity : BaseActivity<LoginOutPresenter>(), SettingContract.View ,
    AppUpdateManager.OnUpdateAppListener {
    val  updateManager =   AppUpdateManager(this)
    var appCheck: AppVerSionBean? =null
    companion object {

        fun start(context: Context) {
            val intent = Intent(context, SettingActivity::class.java)
            context.startActivity(intent)

        }
    }

    override fun initPresenter(): LoginOutPresenter = LoginOutPresenter(this)

    override fun layoutId(): Int = R.layout.act_setting

    override fun initData() {
        presenter?.let {
            updateManager.setOnUpdateAppListener(this)
            updateManager.checkApkVersion(it.getCompositeDis())
        }
        cash.setOnClickListener {
            presenter?.getBankInfo()
        }
        modify_password.setOnClickListener {
            ModifyPasswordActivity.start(this)
        }
        modify_money_password.setOnClickListener {
            ModifyMoneyPasswordActivity.start(this)
        }
        service.setOnClickListener {
            ServiceActivity.start(this)
        }
        version.setOnClickListener {
           appCheck?.let {
               updateManager.showUpdataInfo(it)
           }?:let {
               showToast("当前已是最新版本")
           }
        }
        submit_bt.setOnClickListener {
            var dialog = AlertDialog.Builder(this)
            dialog.setTitle("提示")
            dialog.setMessage("确定退出登录吗？")
            dialog.setPositiveButton("确定", object : DialogInterface.OnClickListener {

                override fun onClick(dialog: DialogInterface?, which: Int) {
                    presenter?.loginOut()
                }

            })
            dialog.setNegativeButton("取消", object : DialogInterface.OnClickListener {

                override fun onClick(dialog: DialogInterface, which: Int) {
                    dialog.dismiss()
                }

            })
            dialog.create().show()
        }
        our_value.text = "V ${AppUtils.getVerName(this)}"
    }

    override fun initView() {
    }

    override fun start() {
    }

    override fun loginOutSuccess() {
        var isLogin: Boolean by Preference(Constants.ISLOGIN, false)
        isLogin = false
        Constants.userInfoBean = null
        showToast("退出登录")
        MyApp.clearnAll(this)
        startActivity<LoginActivity>()
        finish()
    }

    override fun getBankInfoSuccess(bankCardInfoBean: BankCardInfoBean?) {
        AddBankCardActivity.start(this, bankCardInfoBean)
    }

    override fun getBankInfoFail() {
        AddBankCardActivity.start(this)
    }

    override fun onUpdate(appCheck: AppVerSionBean) {
        this.appCheck =appCheck
        version_tv.text = "发现新版本"
        version_tv.setTextColor(resources.getColor(R.color.task_3_color))
    }

    override fun onNoupdate() {
    }

}